import os
import glob
import collections
import fire



def split_name(img_path, save_path):
    save_file = os.path.join(save_path, 'with.txt')

    os.chdir(img_path)
    all_names = glob.glob(os.path.join(img_path, '*.jpg'))
    name_list = []
    quexian_list = []
    li = []
    pos_li = []
    for lii in range(0,10):
        li.append(str(lii))
    for lich in ['_', 'LRP']:
        li.append(lich)
    for polii in range(1,27):
        pos_li.append(str(polii))
    for polich in ['A', 'B', 'C', 'D', 'E', 'F']:
        pos_li.append(polich)    
    former_name = ''
    for file_name in all_names:
        first_name = os.path.basename(file_name).split('.jpg')[0]
        later_name = ''
        new_name = ''
        for ch in first_name:
            if ch not in li:
                print(new_name)
                later_name = first_name.split(new_name)[-1]
                break
            new_name += ch
        if new_name == former_name:
            # 防止一张图出现两遍
            new_name = ''.join([new_name, '9'])
        former_name = new_name
        name_list.append(new_name)

        # quexian_pos = ''
        # for i in range(len(later_name)):
        #     if later_name[i] in pos_li:
        #         if i == 0:
        #             if later_name[i+1] in pos_li:
        #                 quexian_pos.append(later_name[i])
        #             # else:
        #             #     continue
        #         elif i > 0 and i < len(later_name)-1:
        #             if later_name[i-1] in pos_li or later_name[i+1] in pos_li:
        #                 quexian_pos.append(later_name[i])
        #             # else:
        #             #     continue                        
        #         elif i == len(later_name)-1:
        #             if later_name[i-1] in pos_li:
        #                 quexian_pos.append(later_name[i]) 
        #             # else:
        #             #     continue
        #     # else:
        #     #     continue
        # quexian_list.append(quexian_pos)

        quexian_list.append(later_name)
        switch_name = '.'.join([new_name, 'jpg'])
        os.rename(os.path.basename(file_name), switch_name)

    with open(save_file, 'a') as f:
        for i in range(len(name_list)):
            print('{} {}\n'.format(name_list[i], quexian_list[i]))
            st = '{} {}\n'.format(name_list[i], quexian_list[i])
            f.write(st)



if __name__ == '__main__':
    fire.Fire(split_name)